#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
void test_buyCard(struct gameState *state);
void test_drawCard(struct gameState *state);
void test_discardCard(struct gameState *state);
void getRandKingdomCards(int *kingdomCards);
int stillAvailable(int card, struct gameState *state);

void test_buyCard(struct gameState *state){
	struct gameState *oldState = newGame();
    *oldState = *state;
    int card;
//Case 1: Try to buy a card with enough money and the card still available
    card = (rand() % 27);
    while(!(stillAvailable(card, state))){card = (rand() % 27);}
    buyCard(card,state);
    if (state->numBuys == (oldState->numBuys-1) &&
        state->supplyCount[card] == (oldState->supplyCount[card]-1) &&
        state->coins == (oldState->coins - getCost(card)))
    {
        printf("The first buyCard test has passed\n");
    }
    else
    {
        printf("The first buyCard test has failed\n");
    }
    //memcpy(oldState,state,sizeof(oldState));

//Case 2: Try to buy a cad without enough coins
    state->coins = 0;
    if(buyCard(card,state) == 0)
    {
        printf("The second buyCard test failed by not returning -1 when state->coins was 0.\n");
    }
    else
    {
        printf("The second buyCard test passed by returning a -1 when state->coins = 0\n");
    }
    //memcpy(oldState,state,sizeof(oldState));
//Case 3: card is sold but the player has enough money
    state->supplyCount[card] = 0;
    if(buyCard(card,state) == 0)
    {
        printf("The third buyCard test failed by not returning -1 when state->supplyCount[card] = 0.\n");
    }
    else
    {
        printf("The third buyCard test passed by returning a -1 when state->supplyCount[card] = 0\n");
    }
    free(oldState);
}

void test_drawCard(struct gameState *state)
{
    struct gameState *oldState = newGame();
    int i;
    for(i=0;i<10;i++)
    {
        state->deck[0][i] = i;
        state->discard[0][i] = i+10;
    }
    state->deckCount[0] = 10;
    state->discardCount[0] = 10;
    *oldState = *state;
//Case 1: cards int deck
    drawCard(0,state);
    if((state->deckCount[0] == oldState->deckCount[0]-1 ||
        state->deckCount[0] == (oldState->deckCount[0]
        + oldState->discardCount[0] -1)) &&
        state->handCount[0] == oldState->handCount[0]+1)
    {
        printf("The first drawCard test passed\n");
        *oldState = *state;
    }
    else
    {
        printf("The first drawCard test failed\n");
        *state = *oldState;
    }
    //Put all cards from deck into discard
    for(i=0;i<state->deckCount[0];i++)
    {
        state->discard[0][state->discardCount[0]] = state->deck[0][i];
        state->discardCount[0]++;
    }
    state->deckCount[0] = 0;
    *oldState = *state;
//Case 2: no cards in deck
    drawCard(0, state);
    if((state->deckCount[0] == oldState->deckCount[0]-1 ||
        state->deckCount[0] == (oldState->deckCount[0]
        + oldState->discardCount[0] -1)) &&
        state->handCount[0] == oldState->handCount[0]+1)
    {
        printf("The second drawCard test passed\n");
        *oldState = *state;
    }
    else
    {
        printf("The second drawCard test failed\n");
    }

//Case 3: deck and discard are empty
    state->deckCount[0] = 0;
    state->discardCount[0] = 0;
    if(drawCard(0,state) == -1)
    {
        printf("The third drawCard test passed by returning -1\n");
    }
    else
    {
        printf("The third drawCard test failed by not returning -1\n");
    }
    *state = *oldState;
    free(oldState);
}

void test_discardCard(struct gameState *state)
{
    struct gameState *oldState = newGame();
    *oldState= *state;
    int handPos;
    handPos = (rand()%state->handCount[0]);

//Case 1: No trash, value is within hand size
    discardCard(handPos,0,state,0);
    if(state->discardCount[0] == oldState->discardCount[0] + 1 &&
       state->discard[0][state->discardCount[0]-1] == oldState->hand[0][handPos] &&
       state->handCount[0] == (oldState->handCount[0]-1))
       {
            if(state->handCount[0] == 0)
            {
                printf("The first discardCard test passed\n");
            }
            else
            {
                if(state->hand[0][handPos] == oldState->hand[0][oldState->handCount[0]-1])
                {
                    printf("The first discardCard test passed and replaced the card with the last card in the hand\n");
                }
                else
                {
                    printf("The first discardCard test failed because it didnt' replace the discarded card with the last card in the hand\n");
                }
            }

       }
    else
    {
        printf("The First discardCard test Failed\n");
    }
    *state = *oldState;
//Case 2: Trash card is set
    discardCard(handPos,0,state,1);
    if(state->discardCount[0] == oldState->discardCount[0] &&
       state->handCount[0] == (oldState->handCount[0]-1))
       {
            if(state->handCount[0] == 0)
            {
                printf("The Second discardCard test passed\n");
            }
            else
            {
                if(state->hand[0][handPos] == oldState->hand[0][oldState->handCount[0]-1])
                {
                    printf("The second discardCard test passed and replaced the card with the last card in the hand\n");
                }
                else
                {
                    printf("The second discardCard test failed because it didnt' replace the discarded card with the last card in the hand\n");
                }
            }

       }
    else
    {
        printf("The second discardCard test Failed");
    }

    free(oldState);
}

void getRandKingdomCards(int *kingdomCards)
{
    int i,j, temp;
    for (i=0;i<10;i++)
    {
        temp = (rand() % 20 + 7);
        for (j=0;j<=i;j++)
        {
            if (temp == kingdomCards[j])
            {
                i--;
                break;
            } else if (j==i){kingdomCards[i] = temp;}
        }
    }
}
int stillAvailable(int card, struct gameState *state)
{
    if(state->supplyCount[card] > 0){ return 1;} else{return 0;}
}

int main(int argc, char *argv[])
{
    struct gameState *state = newGame();
    int i,card,temp,temp2,temp3,numPlayers,kingdomCards[10],curPlayer;
    int buyableCards[13];
    getRandKingdomCards(kingdomCards);
    state->supplyCount[copper] = 60;
    state->supplyCount[silver] = 40;
    state->supplyCount[gold] = 30;
    state->supplyCount[estate] = 12;
    state->supplyCount[duchy] = 12;
    state->supplyCount[province] = 12;
    for(i=0;i<10;i++)
    {
        buyableCards[i] = kingdomCards[i];
        state->supplyCount[kingdomCards[i]] = 10;
    }
    buyableCards[10]=copper;
    buyableCards[11]=silver;
    buyableCards[12]=gold;
    state->whoseTurn = 0;
    state->numPlayers = 2;
    state->numActions = 1;
    state->numBuys = 1;
    for(i=0;i<6;i++)
    {
        state->hand[0][i] = silver;
        state->hand[1][i] = silver;
    }
    state->handCount[0] = 5;
    state->handCount[1] = 5;
    state->coins = 10;

    test_buyCard(state);
    test_drawCard(state);
    test_discardCard(state);
//    test_gainCard();

    free(state);
	return 0;
}

